package dk.eclipse.android.badassapp.sensor.orientation;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import dk.eclipse.android.badassapp.R;

/**
 * This activity explores the sensor API and services.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 */
public class OrientationActivity extends Activity implements
		OrientationListener {
	private static final String NOT_AVAILABLE = "N/A";

	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = OrientationActivity.class
			.getSimpleName();

	private static Context CONTEXT;

	private TextView orientationAzimuthTextView;
	private TextView orientationPitchTextView;
	private TextView orientationRollTextView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.sensor_orientation);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_app);

		orientationAzimuthTextView = (TextView) findViewById(R.id.orientationAzimuth);
		orientationPitchTextView = (TextView) findViewById(R.id.orientationPitch);
		orientationRollTextView = (TextView) findViewById(R.id.orientationRoll);

		CONTEXT = this;

		String initValue = "0";
		if (!OrientationManager.isSupported()) {
			initValue = NOT_AVAILABLE;
		}
		orientationAzimuthTextView.setText(initValue);
		orientationPitchTextView.setText(initValue);
		orientationRollTextView.setText(initValue);

	}

	protected void onResume() {
		super.onResume();
		if (OrientationManager.isSupported()) {
			OrientationManager.startListening(this);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
		if (OrientationManager.isListening()) {
			OrientationManager.stopListening();
		}
	}

	public static Context getContext() {
		return CONTEXT;
	}


	@Override
	public void onOrientationChanged(float azimuth, float pitch, float roll) {

		orientationAzimuthTextView.setText(String.valueOf(azimuth));
		orientationPitchTextView.setText(String.valueOf(pitch));
		orientationRollTextView.setText(String.valueOf(roll));
	}

	@Override
	public void onBottomUp() {
		Toast.makeText(this, "Bottom UP", 1000).show();
	}

	@Override
	public void onLeftUp() {
		Toast.makeText(this, "Left UP", 1000).show();
	}

	@Override
	public void onRightUp() {
		Toast.makeText(this, "Right UP", 1000).show();
	}

	@Override
	public void onTopUp() {
		Toast.makeText(this, "Top UP", 1000).show();
	}
	
}